from django.shortcuts import render,redirect
from db import  models
import time
from showlog import settings

# Create your views here.


def hostdel(request):
    if request.method == "GET":
        id = request.GET.get("id")
        models.host_db.objects.filter(id=id).delete()
        return redirect("/host/hostlist/")

def hostlist(request):
    if request.method == "GET":
        host_list = models.host_db.objects.values()
        info={}
        info={"host_list":host_list}
        # static_path = settings.STATICFILES_DIRS
        # a = "/data/log/t1.log"
        # print(a.split("/")[-1])

        if host_list.count() == 0:
            return redirect("/host/hostadd/")
        else:
            return render(request, "hostlist.html",info)
    else:
        return redirect("/host/hostadd/")

def hostadd(request):
    if request.method == "GET":
        return render(request, "hostadd.html")
    else:
        print("++++++++++++++add+++++++++++++++++")
        ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        host_ip = request.POST.get("host_ip")
        host_log_path = request.POST.get("host_log_path")
        host_user = request.POST.get("host_user")
        host_name = request.POST.get("host_name")
        host_password = request.POST.get("host_password")
        models.host_db.objects.create(host_ip=host_ip,host_name=host_name,host_log_path=host_log_path,host_ctime=ctime,host_password=host_password,host_user=host_user)
        info={"result":"添加完成"}
        return redirect("/host/hostlist/")

